package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
public class DishController  {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishService dishService;

    @PostMapping
    public R save(DishDto dishDto){
        dishService.saveDishWithFlavors(dishDto);
        return R.success();
    }

    @GetMapping("/page")
    public R<Page> page(@RequestParam(required = false,defaultValue = "1") int page,
                        @RequestParam(required = false,defaultValue = "10")int pageSize, String name){
        Page<Dish> pageInfo =new Page<>(page,pageSize);


        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        lambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);

        dishService.page(pageInfo,lambdaQueryWrapper);

        Page<DishDto> dishDtoPage = new Page<>();

        dishDtoPage.setTotal(pageInfo.getTotal());

        List<Dish> dishlist = pageInfo.getRecords();

        List<DishDto> dishDtolist = dishlist.stream().map(dish -> {
            DishDto dishDto = new DishDto();

            BeanUtils.copyProperties(dish, dishDto);

            Category category = categoryService.getById(dish.getCategoryId());
            if (category != null) {
                dishDto.setCategoryName(category.getName());
            }
            return dishDto;
        }).collect(Collectors.toList());

        dishDtoPage.setRecords(dishDtolist);

        return R.success(dishDtoPage);
    }

    @GetMapping("/{id}")
    public R<DishDto> getByid(Long id){
        DishDto dishDto= dishService.getByIdWithFlavor(id);
        if (dishDto!=null){
            return R.success(dishDto);
        }
        return R.error("未查询到菜品");
    }

    @PutMapping
    public R update(@RequestBody DishDto dishDto){

        dishService.updateWithFlavor(dishDto);

        return R.success();
    }

    @GetMapping("/list")
    public R<List<Dish>> list(Dish dish){
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(dish.getCategoryId()!= null,Dish::getCategoryId,dish.getCategoryId());
        lambdaQueryWrapper.eq(Dish::getStatus,1);
        lambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);

        List<Dish> dishList = dishService.list(lambdaQueryWrapper);

        return R.success(dishList);
    }
}
